#!/bin/bash

service_dir=$1
arg1=$2
arg2=$3

# 合并依赖
cp /data/app/mod_lib/*.jar /data/app/lib/

if [ "${service_dir}" = "${service_dir#/}" ]; then
  # 本地运行，非绝对路径，使用当前路径补全
  service_dir=`pwd`/$service_dir
fi

cd $service_dir

DOCKER_MEM_FILE='/sys/fs/cgroup/memory/memory.limit_in_bytes'
launcher="org.springframework.boot.loader.JarLauncher"

if [[ -e ${DOCKER_MEM_FILE} ]]; then
  MEM=$(($(cat "$DOCKER_MEM_FILE") / 1048576))
  XMX=$((MEM / 2))
  XMS=$((XMX / 4))
  exec java -Xmx"$XMX"m -Xms"$XMS"m ${launcher} ${arg1} ${arg2}
else
  # DEBUG_ARGS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9502,server=y,suspend=y"
  exec java ${launcher} ${arg1} ${arg2}
fi
